package com.ciwong.epaper.modules.pad.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.util.MeUtil;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.ToastUtil;

/**
 * Created by Administrator on 2017/7/12.
 */
public class PadReSetPwdActivity extends Activity implements View.OnClickListener {


	//    private ViewGroup mContentView;
	private EditText mEtNewPwd;
	private EditText mEtConfirmPwd;
	private TextView mTvComplete;
	private String   psw1, psw2;
	private String phoneNumber, verityCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_pad_reset_pwd_dialog);


		Intent intent = getIntent();


		if (null != intent) {

			phoneNumber = intent.getStringExtra("phonenumber");

			verityCode = intent.getStringExtra("verityCode");

		}

		setFinishOnTouchOutside(false);
		//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
		initView();
		initEvent();
	}


	//    public void setPhoneNumANdVertiyCode(String phoneNumber,String verityCode){
	//        this.phoneNumber=phoneNumber;
	//        this.verityCode = verityCode;
	//    }


	//    public CWReSetPwdDialog(@NonNull Context context, @StyleRes int themeResId) {
	//        super(context, themeResId);
	//        mContentView = (ViewGroup) LayoutInflater.from(context)
	//                .inflate(R.layout.layout_pad_reset_pwd_dialog, null);
	//
	//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	//        super.setContentView(mContentView);
	//
	//
	//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	//        initView();
	//        initEvent();
	//    }


	private void initView() {
		mEtNewPwd = (EditText) findViewById(R.id.et_new_password);
		mEtConfirmPwd = (EditText) findViewById(R.id.et_cofirm_password);
		mTvComplete = (TextView) findViewById(R.id.tv_complete);

	}

	private void initData() {

	}

	private void initEvent() {
		mTvComplete.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id ==R.id.tv_complete){
			resetpsw();
		}

	}

	private void resetpsw() {
		psw1 = mEtNewPwd.getText().toString();
		psw2 = mEtConfirmPwd.getText().toString();
		String tips = "";
		if (!TextUtils.isEmpty(phoneNumber) && !TextUtils.isEmpty(verityCode)) {
			if (!MeUtil.validatePassword2(psw1)) {
				tips = getString(R.string.password_not_corret);
				showToastError(tips);
			} else if (!MeUtil.validatePassword2(psw1)) {
				tips = getString(R.string.password_not_corret);
				showToastError(tips);
			} else if (!psw1.equals(psw2)) {
				tips = getString(R.string.psw_should_same);
				showToastError(tips);
			} else if (!NetworkUtils.isOnline()) {
				tips = getString(R.string.request_failed);
				showToastError(tips);
			} else {
				reSetPassword(EApplication.BRAND_ID + "", phoneNumber, verityCode, psw1, psw2);
			}

		}
	}

	private void reSetPassword(String brandId, String phoneNumber, String verityCode, String password, String confirmPassword) {

		MeDao.getInstance().reSetPSW(brandId, phoneNumber, verityCode, password, confirmPassword, new BaseCallBack(){

			@Override
			public void failed(int errorCode, Object data) {
				if(data!=null){
					showToastError(data.toString());
				}
			}

			@Override
			public void failed(Object data) {
				if (data instanceof TimeoutError) {
					showToastError("连接超时，请稍后重试");
				}
			}

			@Override
			public void success(Object data) {
				Boolean b=(Boolean)data;
				if(b){
					Intent intent = new Intent(PadReSetPwdActivity.this, PadSuccessActivity.class);
					startActivityForResult(intent, RequestCode.PadResquestCode.REQ_CODE_TO_LOGIN);
				}
			}
		});




	}


	/**
	 * 吐丝显示错误信息(方块)
	 *
	 * @param msg 字符串
	 */
	public void showToastError(String msg) {
		ToastUtil.INSTANCE.toastInCenter(PadReSetPwdActivity.this,msg);

	}


	public boolean isShouldHideInput(View v, MotionEvent event) {
		if (v != null && (v instanceof EditText)) {
			int[] leftTop = {0, 0};
			//获取输入框当前的location位置
			v.getLocationInWindow(leftTop);
			int left = leftTop[0];
			int top = leftTop[1];
			int bottom = top + v.getHeight();
			int right = left + v.getWidth();
			if (event.getX() > left && event.getX() < right
					&& event.getY() > top && event.getY() < bottom) {
				// 点击的是输入框区域，保留点击EditText的事件
				return false;
			} else {
				return true;
			}
		}
		return false;
	}


	private void hideSoftInputWindow(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null) {
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
	}

	InputMethodManager manager = null;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (manager == null) {
			manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == RequestCode.PadResquestCode.REQ_CODE_TO_LOGIN) {
				setResult(RESULT_OK);
				finish();
			}
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.alpha = 0.0f;
		getWindow().setAttributes(lp);
	}

	@Override
	protected void onResume() {
		super.onResume();
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.alpha = 1.0f;
		getWindow().setAttributes(lp);

	}
}
